package com.wltt.product.ahuthr.config.filter;

import com.wltt.product.ahuthr.component.SystemUserComponent;
import com.wltt.product.ahuthr.constants.AuthConstant;
import jakarta.annotation.Resource;
import jakarta.servlet.Filter;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnProperty(
        prefix = "auth",
        name = "switch",
        havingValue = AuthConstant.AUTH_ON
)
public class AuthConfig {

    private static final Logger logger = LogManager.getLogger(AuthConfig.class);

    @Resource
    private SystemUserComponent systemUserComponent;

    @Bean
    public FilterRegistrationBean authFilter() {
        logger.info("登录认证开启");
        FilterRegistrationBean<Filter> filterBean = new FilterRegistrationBean();
        filterBean.setName("authFilter");
        filterBean.setFilter(new AuthFilter(systemUserComponent));
        filterBean.setOrder(-10);
        String[] patterns = new String[]{"/api/*"};
        filterBean.addUrlPatterns(patterns);
        filterBean.addInitParameter("excludePaths", "/api/open/");
        return filterBean;
    }

}
